package com.dmxtechnologies.community.ejb.interfaces.impl;

import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.dmxtechnologies.community.ejb.interfaces.WifiRealTimeBroadcastRemote;
import com.dmxtechnologies.community.pojo.CMSDetailResponse;
import com.dmxtechnologies.community.rar.api.CMSBaseRequest;
import com.dmxtechnologies.community.rar.api.CMSConnection;
import com.dmxtechnologies.community.rar.api.CMSConnectionFactory;

/**
 * Session Bean implementation class RealTimeBroadcast
 */
@Singleton
@Startup
public class WifiRealTimeBroadcast implements WifiRealTimeBroadcastRemote {
	private static final Logger log = Logger.getLogger("RealTimeBroadcast");
	@Resource(lookup = "java:global/env/eis/DMXCMSConnectionFactory")
	private CMSConnectionFactory connectionFactory;

	private CMSDetailResponse detailResponse = null;
	
    /**
     * Default constructor. 
     */
    public WifiRealTimeBroadcast() {
    }
    
	@Schedule(minute = "*/55", hour = "*", persistent = false)
	public void timer() throws Exception {
		log.info("enter");
		// 229759_00406001_3047_47125_1
		CMSBaseRequest request = new CMSBaseRequest();
		request.setCommunity("229759");
		request.setPreColumns("00406001");
		request.setTemplateExample("3047");
		request.setNextColumns("47125");
		request.setPagination(1);
		log.info("[ResourceAccessBean] Getting connection from the RA");
		try (CMSConnection connection = connectionFactory.getConnection()) {
			detailResponse = (CMSDetailResponse) connection
					.getContentDetailRequestToCMS(request);
			log.info("invoke success");
		}
	}

	@Override
	public CMSDetailResponse getWifiList() throws Exception {
		if(detailResponse == null){
			this.timer();
		}
		return detailResponse;
	}
}
